// pages/Main/mainIndex/mainIndex.js
var businessLogic = require('../../../utils/NetWork/Model/LoginbusinessLogic.js')
var common = require('../../../utils/common.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectRole: true,
    firstRole: '请选择登录角色',
    selectArea: false,
    orgid: "",
    userid: "",
    ryid: "",
    content:[],
    array:[],
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value,
    })
    console.log('picker发送选择改变，携带值为', this.data.array[this.data.index] ); 
    // this.loginBtnClick()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var user=common.getStorageSync('user');
    console.log(user);
    var account = [];
    for(let a = 0 ; a < user.length;a++){
      account[a] = user[a].account;
    }
    account.unshift("请选择登录账号");
    console.log(account);
    this.setData({
      index: 0,
      array: account
    })  
    // this.loginBtnClick();
    this.list();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }, 
  passWdInput: function (e) {
    this.setData({
      userPwd: e.detail.value
    })
  }, 
  //获取用户输入的密码
  loginBtnClick: function (e) {
    var i = this.data.index
    // console.log("用户名：" + this.data.userName + " 密码：" + this.data.userPwd);
    if (this.data.array[this.data.index] == "请选择登录账号" || this.data.userPwd == undefined) {
      common.showToast("请绑定账号", "none")
      setTimeout(function () {
        //要延时执行的代码
        wx.reLaunch({
          url: '../boundwx/boundwx'
        })
      }, 1000) //延迟时间
    } else {
      let params = { "username": this.data.array[this.data.index] , password: this.data.userPwd };
      //第三步 调用接口
      businessLogic.userLogin(params, (res) => {
        // console.log(res);
        if (res.flag == 0) {
          common.showToast(res.msg, "none");
        } else {
          common.showToast(res.msg, "success");
          common.setStorageSync("userid", res.userId);
          common.setStorageSync("orgid", res.orgid);
          common.setStorageSync("rid", res.rid);
          var openindex = this.data.index;
          common.setStorageSync("openindex", openindex-1);
          
          // common.setStorageSync("openid", res.openid);
           wx.reLaunch({
           url: '../../Main/teacherOrder/teacherOrder',
          }) 
        }

      });

    } 
   
  },  

  
   list:function(e){

     let params = { "userid": this.data.userid, "orgid": this.data.orgid, "searchField": "openid", "searchValue": "" + this.data.ryid+ "" }
    //第三步 调用接口
    businessLogic.weixinhao(params, (res) => {
      console.log("6.3.1", res)
       //第五步 把获取的值，传入data
     
      
    
    });

  }, 

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }, register: function (e) {
    wx.navigateTo({
      url: '../register/register',
    })
  }, forgetPWD: function (e) {
    wx.navigateTo({
      url: '../forgetpassword/forgetpassword',
    })
  }, 
  teacherOrder: function (e) {
    wx.navigateTo({
      url: '../../Main/teacherOrder/teacherOrder',
    })
  }, 
   clickRole: function () {
    var selectRole = this.data.selectRole;
    if (selectRole == true) {
      this.setData({
        selectArea: true,
        selectRole: false,
      })
    } else {
      this.setData({
        selectArea: false,
        selectRole: true,
      })
    }
  }, mySelect: function (e) {
    this.setData({
      firstRole: e.target.dataset.me,
      selectRole: true,
      selectArea: false,
    })
  }, send: function (e) {

  } 
})